package com.jiutong.meipenggang.ui.activitys;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.present.UserInfoPresent;
import com.jiutong.meipenggang.utils.NumUilt;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.utils.StringUtil;
import com.jiutong.meipenggang.utils.TimerUtil;
import com.jiutong.meipenggang.views.dialog.BaseDialog;

import butterknife.Bind;
import butterknife.OnClick;

public class ChangePhoneActivity extends BaseActivity implements RegisterAndLoginBiz {

    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.tv_change_phone_old)
    TextView tvChangePhoneOld;
    @Bind(R.id.et_change_phone_verify_code)
    EditText etChangePhoneVerifyCode;
    @Bind(R.id.tv_change_phone_send_code)
    TextView tvChangePhoneSendCode;
    @Bind(R.id.et_change_phone_new)
    EditText etChangePhoneNew;
    @Bind(R.id.btn_change_phone_confirm)
    Button btnChangePhoneConfirm;
    private String phone;
    private TimerUtil timerUtil;

    private String code;
    private BaseDialog dialog;

    private UserInfoPresent userInfoPresent;


    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_change_phone;
    }

    @Override
    protected void initView() {
        super.initView();

        tvCommonTitle.setText("修改手机号");
        phone = SpConfig.getInstance().getString(Constants.USERNAME_STRING);
        tvChangePhoneOld.setText(NumUilt.phoneFormat(phone));
//        btnChangePhoneConfirm.setClickable(false);

        timerUtil = new TimerUtil(tvChangePhoneSendCode);
        timerUtil.setCountTimer();
        userInfoPresent = new UserInfoPresent(this, this);
    }


    @OnClick({R.id.rl_common_title_back, R.id.tv_change_phone_send_code, R.id.btn_change_phone_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.tv_change_phone_send_code:
                userInfoPresent.getAuthCode();
                timerUtil.startTimer();
                break;
            case R.id.btn_change_phone_confirm:
                if(isRight()){
                    showDialog();
                }
                break;
            default:
                break;
        }
    }

    private void showDialog() {
        if (dialog == null) {
            dialog = new BaseDialog.Builder().
                    layResuorce(R.layout.dialog_common).
                    cancelViewClickEvent(R.id.tv_dialog_cancel).
                    okViewClickEvent(R.id.tv_dialog_ok).
                    confirmId(R.id.tv_dialog_confirm).
                    llBottomId(R.id.ll_dialog).
                    contentTextId(R.id.tv_dialog_content).
                    hintTextId(R.id.tv_dialog_hint).
                    setContentText("您的新手机号码修改成功，请重新登录！").
                    setConfirmText("确定").
                    setHintText("温馨提示").
                    setOkText("是").
                    setCancelText("否").
                    gravityStyle(Gravity.CENTER).
                    showOneOrTwoBtn(false).
                    animResource(R.style.dialog_animation_fade).
                    dialogWidth(-2).
                    outSideCancel(false).
                    build();
            dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                @Override
                public void onOkClick() {
                }

                @Override
                public void onOk2Click() {
                    dialog.dismiss();
                    SpConfig.getInstance().edit().putString(Constants.TEPPHONE_STRING,etChangePhoneNew.getText().toString().trim()).commit();
                    SpConfig.getInstance().edit().putString(Constants.USERNAME_STRING,etChangePhoneNew.getText().toString().trim()).commit();
                    SpConfig.getInstance().putBool(Constants.IS_LOGIN, false);
                    openActivity(LoginActivity.class);
                    AppManager.getAppManager().finishActivity(ChangePhoneActivity.this);
                }
            });
        }

        dialog.show(getSupportFragmentManager(), TAG);
    }

    private boolean isRight() {
        if (TextUtils.isEmpty(getVerificatCode())) {
            toastShortShow("请输入验证码");
            return false;
        }
        if (!code.equals(getVerificatCode())) {
            toastShortShow("验证码错误");
            return false;
        }
        if (TextUtils.isEmpty(etChangePhoneNew.getText().toString().trim())) {
            toastShortShow("请输入新的手机号");
            return false;
        }
        if (!StringUtil.isMobileNumber(etChangePhoneNew.getText().toString().trim())) {
            toastShortShow("请输入正确的手机号");
            return false;
        }
        return true;
    }


    @Override
    public String getPhoneNum() {
        return phone;
    }

    @Override
    public String getVerificatCode() {
        return etChangePhoneVerifyCode.getText().toString().trim();
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(BaseBean subjects) {

    }


    @Override
    public void onSuccess(Object object) {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }
}
